import Conduct from './conduct.js'
import UtilFuncs from '../common/tools/utilfuncs.js'
var funcs = UtilFuncs.UtilFuncs;

import GlobalConst from '../common/globalConst.js'
var global = GlobalConst.GlobalConst;

import {LGXEnums}  from '../common/sysenum.js';


import topoconnline from '../common/struct/basestruct/topoconnline.js';

class Connline extends Conduct.Conduct {
    constructor() {
        super();
        this.stNode = null;
        this.endNode = null;
        this.type = "connline";
    }

    setStNode(node) {
        this.stNode = node;
    }

    getStNode() {
        return this.stNode;
    }

    setEndNode(node) {
        this.endNode = node;
    }

    getEndNode() {
        return this.endNode;
    }

    addNode4Edit(pNode, mousepoint) {
        if (pNode) {
            if (this.m_connNodeList.indexOf(pNode) == -1) {
                if (this.m_connNodeList.length < 2) {
                    //线最多只有两个连接点
                    this.m_connNodeList.push(pNode);
                    pNode.addConnLine(this);
                }
            }

            let graph = this.m_refGraph;
            if (graph.m_graphType != LGXEnums.LGXGraphType.LineGraph_type) {
                return;
            }
            let position = graph.getPosCopy();
            let headPt = position.getHead();
            let tailPt = position.getTail();
            let dis1 = funcs.distancePoint(headPt, mousepoint, 1);
            let dis2 = funcs.distancePoint(tailPt, mousepoint, 1);
            if (dis1 < dis2) {
                this.stNode = pNode;
            }
            else {
                this.endNode = pNode;
            }
        }
    }

    addNode(pNode) {
        if (pNode) {
            if (this.m_connNodeList.indexOf(pNode) == -1) {
                if (this.m_connNodeList.length < 2) {
                    this.m_connNodeList.push(pNode);
                }
            }
        }
    }

    removeNode(pNode) {
        if (pNode) {
            var pos = this.m_connNodeList.indexOf(pNode);
            if (pos >= 0) {
                this.m_connNodeList.splice(pos, 1);
                pNode.removeLineSeg(this);
                if (this.endNode == pNode) {
                    this.endNode = null;
                }
                else if (this.stNode == pNode) {
                    this.stNode = null;
                }
            }
        }
    }

    clearNode() {
        var nOldCount = this.m_connNodeList.size();
        for (var i = nOldCount - 1; i >= 0; i--) {
            var pNode = this.m_connNodeList[i];
            this.removeNode(pNode);
        }
    }

    getLnEndConnNode(endPoint, scaling) {
        var pEndNode = null;
        var nodePt;
        var nOldCount = this.m_connNodeList.size();
        for (var i = nOldCount - 1; i >= 0; i--) {
            var pNode = this.connNodeList[i];
            nodePt = pNode.getPos();
            var dis = funcs.distancePoint(endPoint, nodePt, scaling);
            if (dis >= 0 && dis < global.SelectGap) {
                pEndNode = pNode;
                break;
            }
        }
        return pEndNode;
    }

    getNodeInListPos(node) {
        return this.m_connNodeList.indexOf(node);
    }
    getNodeCount() { return this.m_connNodeList.length; }

    toInfo() {
        let info = new topoconnline.TopoConnline();
        info.id = this.id;
        if (this.m_refGraph) {
            info.refID = this.m_refGraph.m_rid;
        }
        else {
            this.setUseful(0);
        }
        if (this.stNode) {
            info.stNodeID = this.stNode.id;
        }
        if (this.endNode) {
            info.endNodeID = this.endNode.id;
        }
        return info;
    }

    notifyPosChang(preCnt, curentCnt,mapscale,inMulSelRegionLineLst) {
        //连接线自身没有连接点，不需要做联动修改
    }
}

export default { Connline }







